<script>
import { defineComponent } from "vue";
// import { getAddCart } from "../../api/pro";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { getCategories, getAddimg } from "../../api/pro"
import { Plus } from "@element-plus/icons-vue";
const router = useRouter()

export default defineComponent({
    data() {
        return {

            activeIndex: '0',

            //添加商品的表单数据对象
            addForm: {
                goods_name: '',
                goods_price: 0,
                goods_weigth: 0,
                goods_number: 0,


            },
            addFormrules: {
                goods_name: [
                    { required: true, message: '请输入商品名称', trigger: 'blur' }
                ],
                goods_price: [
                    { required: true, message: '请输入商品价格', trigger: 'blur' }
                ],
                goods_weigth: [
                    { required: true, message: '请输入商品重量', trigger: 'blur' }
                ],
                goods_number: [
                    { required: true, message: '请输入商品数量', trigger: 'blur' }
                ],

            },
            Gories: [],

            value: '',
            options: [],

            props: {
                expandTrigger: 'hover',
            },
            newArray: [],
            imageUrl: '',

            formData: {
                file: '',
            },



        }
    },
    mounted() {
        getCategories().then((res) => {
            console.log(res.data.data)
            this.Gories = res.data.data
            // console.log('请求获取的', this.Gories)
            function test(tree) {
                const outArray = []
                tree.forEach(item => {
                    const out = {
                        value: item.cat_id,
                        label: item.cat_name
                    }
                    if (item.children && item.children.length) {
                        out.children = test(item.children)
                    }
                    outArray.push(out)
                })
                return outArray
            }

            this.options = test(this.Gories)
            console.log('处理之后的数据', this.options);


            if (res.data.meta.status !== 200) {
                return ElMessage.error('获取商品分类数据失败')
            }
            ElMessage.success(res.data.meta.msg)
        })
        tinymce.init({
            selector: "textarea#default-editor",
            branding: false, // 取消右下角默认提示
            height: 500,
            plugins: "advlist link image lists paste", // 使用插件
            paste_data_images: true, // 支持图片粘贴
        });



    },
    methods: {
        addimg() {
            // console.log('点击添加了商品')
            ElMessage.success('添加成功')
            this.$router.push('/pro/prolist')
            // getAddimg(this.formData).then((res) => {
            //     console.log(res)
            //     if(res.code !== '200'){

            //     }
            //     // ElMessage.success('添加成功')
            //     // this.$router.push('/pro/prolist')
            // })
        },
        httpRequest(options) {
            const reader = new FileReader();
            reader.readAsDataURL(options.file);
            reader.onload = () => {
                this.imageUrl = reader.result;
                this.formData.file = reader.result;
            };
        },
        handleSubmit() {
            // 获取内容的方式
            console.log(tinymce.activeEditor.getContent());
        },
        beforeTabLeave(activeName, oldActiveName) {
            // console.log('即将离开的标签页名字是：' +oldActiveName)
            // console.log('即将进入的标签名字是：' + activeName)
            // return false
            if (oldActiveName == '0' && this.value.length === 0) {
                ElMessage.error('请选择商品分类')
                return false
            }
        }

    },
    components: {
        Plus,
    }


})
</script>


<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: 'home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表管理</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>

            <el-steps :active="activeIndex - 0" finish-status="success">
                <el-step title="基本信息" />
                <el-step title="商品参数" />
                <el-step title="商品属性" />
                <el-step title="商品图片" />

            </el-steps>
            <el-form ref="addFormRef" :model="addForm" :rules="addFormrules" label-width="100px" label-position="top">
                <el-tabs v-model="activeIndex" :tab-position="'left'" class="demo-tabs" :before-leave="beforeTabLeave">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="addForm.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量" prop="goods_number">
                            <el-input v-model="addForm.goods_number" type="number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_price">
                            <el-input v-model="addForm.goods_price" type="number"></el-input>
                        </el-form-item>

                        <el-form-item label="商品重量" prop="goods_weigth">
                            <el-input v-model="addForm.goods_weigth"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类" prop="goods_cat">
                            <el-cascader v-model="value" :options="options" :props="props"></el-cascader>

                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品参数" name="1">
                        <div>
                            <el-checkbox v-model="checked1" label="商品品牌" size="large" checked="true" />
                            <el-checkbox v-model="checked2" label="商品规格尺寸" size="large" />
                        </div>
                        <div>
                            <el-checkbox v-model="checked3" label="商品细节展示" />
                            <el-checkbox v-model="checked4" label="商品材质说明" />
                        </div>
                        <el-checkbox v-model="checked3" label="商品安装说明" />
                        <el-checkbox v-model="checked4" label="商品展示" />
                        <div>
                            <el-checkbox v-model="checked5" label="商品瑕疵" size="small" disabled />
                            <el-checkbox v-model="checked6" label="商品是否违规" size="small" disabled />
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="商品属性" name="2">
                        <div>
                            <h3>商品简介</h3>
                            <textarea id="default-editor" v-model="value"></textarea>
                            <el-button @click="handleSubmit">click</el-button>
                        </div>

                    </el-tab-pane>
                    <el-tab-pane label="商品图片" name="3">
                        <el-form-item label="点击上传图片">

                            <el-upload :http-request="httpRequest" class="avatar-uploader">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                                <el-icon v-else class="avatar-uploader-icon">
                                    <Plus />
                                </el-icon>
                            </el-upload>
                        </el-form-item>
                        <el-button type="添加商品" @click="addimg">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>
    </div>
</template>

<style scoped>
.avatar-uploader .avatar {
    width: 300px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 300px;
    height: 178px;
    text-align: center;
}


.el-card {
    height: 820px;
}

.el-tabs__item {
    margin-top: 30px;
}

.el-tab-pane {
    margin-top: 20px;
}
</style>


